Scripting in MMB is something many users overlook. They think it is too difficult of confusing. I'm here to tell you that it's not. While it can get complicated, the things many users want, like buttons that do more than one thing when clicked, are nothing more than a few easy lines.
MMB's script language is built around four main types of elements: commands, variables, decision structures (doing things based on a variable's value), and loops. There is also a fifth element, called comments, but they have no functions, they only make your script easier to read.
Before we get started making something that really works, lets learn about comments. To add a comment you simply add an asterisk (*). From that point on, nothing else on the line will be read by the MMB, it will just skip to the next line.
Commands
The second easiest elements of scripting are the commands. Commands are simply a special word, called a keyword, followed in parentheses (and quotes) by the information it needs to perform a specific function (the pieces of information are called parameters). With the exception of SysCommand, all commands only perform one function. A command looks something like this:
Keyword("Parameter")
("Keyword" is of course not a real command, but just an
example.)
Most commands require at least one parameter. They are usually things like paths, objects on a page, or variables. Some commands require two parameters, each of which are in quotes and separated by a comma. Each one is not in its own set of parentheses though. All the parentheses do is separate the keyword from its parameters, within the parenthesis commas separate the parameters. So, a command with more than one parameter would look like this:
Keyword("Parameter1","Parameter2")
So, a brief review before we move on, a command is simply a line of script that does a specific function. They are made of the keyword, which starts the command, and parameters. The list of parameters (or pieces of information a command needs to know) is placed in parentheses, and commas separate parameters when there are more than one. The parameters themselves are placed inside quotation marks. Also, some commands don't need any parameters, these are still followed by parentheses, but have nothing inside them (i.e. Run() ).
Variables
Variables are a very important aspect of scripting. They allow you to do more than just execute a series of commands. Variables can be thought of as boxes that hold values. Values are, as you can probably understand, are the information stored in the variable. Imagine you want to keep track of whether a button has been clicked. You would use a variable to tell you.
There are two types of variables, which are very easy to remember - numeric and string. As you can probably tell, numeric variables hold only numbers and string variables hold both numbers and other characters, like a text file. So, MMB can tell the two apart, the names of string variables have to be followed by a dollar sign ($).
But wait! What is a variable name you ask? Simply put, what the MMB uses to remember what each variable is. OK so maybe that wouldn't be that easy. But how about this: If variables are like boxes, the names are like the labels on the boxes. If someone tells you to bring them the box with something specific in it, you wouldn't know what box it was in unless if all the boxes weren't labeled. So a variable name allows MMB to remember what box it put the value (information) in. You can name a variable anything you want, though it can only be alphanumeric characters (that is, letters of the alphabet and numbers) and underscores (_). Also, like I said before, if you want the variable to be a string you need the last character of its name to be a dollar symbol.
So, you know what a variable is, but now you want to know how to user these gifts of God. Well it's pretty easy. When you want to put something in a variable, all you have to do is make a line that starts with a variable name, then put an equal sign, and then the value. (String values must be surrounded by single quotes aka apostrophes (').) Typed out it would look something like this:
**for a numeric
variable
MyVariable=
10
**for a string
variable
MyVariable$=
'My String'
(Notice I used comments at the end of each line. They are there to make the script more understand able. They aren't necessary to set a variable.)
If you want to change the value of a variable later, you do the same thing, entering the new value. You can also the value of one variable copied to another by putting its name in place of the value, like this:
**for a numeric
variable
MyVar2=MyVal
**for a string
variable
MyVar2$=MyVar$
MATH
FUNCTIONS
Now you may be wondering what
good is it to have two different kinds of variables. Why do
you need numeric variables if you can just as easily use a
string to hold the numbers? One of the reasons are math
functions.
The format is almost exactly like kindergarten math - basic
addition, subtraction, multiplication, and division. The only
differences are that you use * for multiplication, and / for
division. Here's an example of how to use math
functions:
A1=10 + 2 **equals 12
A2=A1 - 2 **equals 10
A3=A2 * 2 **equals 20
A4=A3 / 2 **equals 10
Notice that for A2, A3, and A4 we use variables in the equations. That's the beauty of math functions, instead of just taking numbers, it can also use numeric variables. That's why you can't use strings. The math functions can't use letters, because you just can't do math with letters! So numeric variables are used just to make sure there aren't any strings.
Another nice aspect is that you can use the value of the variable you want to store the result in as part of the equation. You do it just like above, here's an example:
A1=A1 + 1
This works because MMB first replaces the variable name in the equation with its value, then doing the equation.
THE LIFE OF A
VARIABLE
So if I have a variable, will I
always be able to open it? Only while the program is running.
When the program closes (either by choice or crashing), the
program removes all its variables from memory. Why can't it
just leave the variables there for later? Because programs use
RAM to store variables. RAM is for short-term storage only.
There is a limited amount of space there, and if every program
kept all its variables there, soon you wouldn't have any
memory left to run things. In fact, even if it could just
leave it there between uses, the computer clears everything
from RAM when it shuts down. But you * can * access a variable
between pages in your project. Also be aware that when you use
the Run Another Project function, (or the RunMBD command,
which does the same thing) unless you use the NEW_WINDOW
parameter, you will lose all the variables you previously had.
To use variables again later you can use the SaveVariable and
LoadVariable commands to save to the registry (which will be
covered in another lesson).
OK, I'm finished with a variable, now what? Don't do anything. In fact, there's nothing you can do other than just leave the variable there. As stated before, when the program closes it will get rid of the variable for you.